var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();

Page({
  data: {
    list:{},
    shopInfo: null,
    type: 0,
    searchValue: '',
    herbList: [],
    selectedHerbList: [],
    selectHerbsInfo: '',
    page: 1,
    limit: 10,
    totalPages: 1,
    hasLogin: false
  },
  init: function () {
    this.setData({
      herbList: [],
      page: 1,
      limit: 10,
      totalPages: 1
    });
  },
  chooseLetter: function (e) {
    this.setData({
      curLetter: null
    });
    var letter = e.currentTarget.dataset.letter;
    console.log(letter);

    // 查找对应的id
    var id = "#letter" + letter;
    const query = wx.createSelectorQuery()  //创建节点查询器 query
    query.select(id).boundingClientRect() //这段代码的意思是选择Id = id的节点，获取节点位置信息的查询请求
    query.selectViewport().scrollOffset()  //这段代码的意思是获取页面滑动位置的查询请求
    query.exec(function (res) {
      // res[0].top       // id节点的上边界坐标
      // res[1].scrollTop // 显示区域的竖直滚动位置
      wx.pageScrollTo({
        scrollTop: res[0].top + res[1].scrollTop,
        duration: 300
      })
    })
  },

  getHerbGenericNameList(reachBottom = false) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.HerbAuthGenericNameSelectList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      searchValue: that.data.searchValue
    }).then(function (res) {
      if (res.errno === 0) {
        let herbList = res.data.list;
        //触底加载新数据并保留老数据
        if (reachBottom) {
          herbList = [...that.data.herbList, ...herbList]
        }
        that.setData({
          herbList: herbList,
          totalPages: res.data.pages
        });
        that.initSelectHerbList();
        wx.hideLoading();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },

  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.SelectbrandList, {
    }).then(function (res) {
      console.log("----------",res)
    that.setData({
      list:res
    })
    })
  },
  onReady: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.init();
    this.getHerbGenericNameList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
    } else {
      return false;
    }
    this.getHerbGenericNameList(true);
  },
  onShow: function () {
    if (app.globalData.hasLogin) {
      this.init();
      this.getHerbGenericNameList();

      let shopInfo = wx.getStorageSync('shopInfo');
      this.setData({
        shopInfo: shopInfo
      });
    }
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  onSearch() {
    this.getHerbGenericNameList();
  },
  selectSeries: function(idx){
    console.log(idx.currentTarget.dataset.word)

  },
  checkedItem: function (event) {
    let itemIndex = event.currentTarget.dataset.index;
    let that = this;
    let target = {};

    this.data.herbList.map(function (element, index, array) {
      element.checked = false;
      return element;
    });

    let tmpHerbData = this.data.herbList.map(function (element, index, array) {
      if (index == itemIndex) {
        element.checked = !element.checked;
        target.checked = element.checked;
        target.herbName = element.herbName;
        target.genericName = element.genericName;
        target.specifications = element.specifications;
        target.describe = element.describe;
      }
      return element;
    });

    let targetSelectedList = this.data.selectedHerbList;
    targetSelectedList = [{
      herbName: target.herbName,
      genericName: target.genericName,
      specifications: target.specifications,
      describe: target.describe
    }]
    wx.setStorageSync('single_selectHerbList', targetSelectedList);

    that.setData({
      herbList: tmpHerbData,
      selectedHerbList: targetSelectedList
    });

    let selectHerbsInfo = this.getSelectHerbsInfo();
    that.setData({
      selectHerbsInfo: selectHerbsInfo
    });
  },
  valueFormat(value) {
    if (!value) {
      return '';
    }
    return value;
  },
  initSelectHerbList() {
    let authSelectHerbList = wx.getStorageSync('single_selectHerbList');
    if (authSelectHerbList) {
      let that = this;
      let tmpHerbData = this.data.herbList.map(function (element, index, array) {
        if (authSelectHerbList.find((t) => t.genericName == element.genericName && that.valueFormat(t.describe) == that.valueFormat(element.describe))) {
          element.checked = true;
        }
        return element;
      });
      that.setData({
        herbList: tmpHerbData,
        selectedHerbList: authSelectHerbList
      });
      let selectHerbsInfo = this.getSelectHerbsInfo();
      that.setData({
        selectHerbsInfo: selectHerbsInfo
      });
    }
  },
  getSelectHerbsInfo() {
    let selectHerbsInfo = this.data.selectedHerbList.map((t) => t.describe).join(',');
    return selectHerbsInfo;
  },
  selectConfirm() {
    let that = this;
    let selectHerbs = this.data.selectedHerbList;
    if (selectHerbs.length <= 0) {
      wx.showModal({
        title: '提示信息',
        content: '您未选择品类',
        showCancel: false
      });
      return;
    }
    let selectHerbsInfo = selectHerbs.map((t) => t.genericName).join(',');

    var pages = getCurrentPages(); // 当前页面
    var beforePage = pages[pages.length - 2]; // 前一个页面
    wx.navigateBack({
      success: function () {
        beforePage.callbackSetSelectHerbList(selectHerbs, selectHerbsInfo); // 执行前一个页面的onLoad方法
      }
    });
  },
})